/*
 * Copyright (C) 2023 Alexander Novikov
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef SERIALIZER_H_
#define SERIALIZER_H_

#include <stdint.h>
#include <stddef.h>
#include <netinet/in.h>

enum Endian {
	Big = 0,
	Little = 1
};

typedef struct {
	uint8_t *buffer;
	size_t offset;
	size_t buffer_size;
} serializer_t;

int serializer_init(serializer_t* serializer);

int serializer_buffer_resize(serializer_t* serializer, size_t size);

int serializer_write_bytes(serializer_t* serializer, uint8_t* data, size_t count);

int serializer_read_bytes(serializer_t* serializer, uint8_t* data, size_t count);

int serializer_write_byte(serializer_t* serializer, uint8_t data);

int serializer_read_byte(serializer_t* serializer, uint8_t* data);

int serializer_write_short(serializer_t* serializer, uint16_t data, int endian);

int serializer_read_short(serializer_t* serializer, uint16_t* data, int endian);

int serializer_write_triad(serializer_t* serializer, uint32_t data, int endian);

int serializer_read_triad(serializer_t* serializer, uint32_t* data, int endian);

int serializer_write_int(serializer_t* serializer, uint32_t data, int endian);

int serializer_read_int(serializer_t* serializer, uint32_t* data, int endian);

int serializer_write_long(serializer_t* serializer, uint64_t data, int endian);

int serializer_read_long(serializer_t* serializer, uint64_t* data, int endian);

int serializer_write_sockaddr(serializer_t* serializer, struct sockaddr_in* addr);

int serializer_read_sockaddr(serializer_t* serializer, struct sockaddr_in* addr);

int serializer_deinit(serializer_t* serializer);

#endif /* SERIALIZER_H_ */
